package com.nova.android.tvlauncher.ui.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.nova.android.tvlauncher.constants.ConstantsAtv.APP_INSTALLED
import com.nova.android.tvlauncher.constants.ConstantsAtv.APP_UNINSTALLED
import com_github_lib_base.ext.isRepeatExcute
import sendLiveEventData


class AppStateChangeReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        val packageName = intent.data?.schemeSpecificPart
        if (!packageName.isNullOrEmpty()) {
            when (action) {
                // 应用被卸载
                Intent.ACTION_PACKAGE_REMOVED -> {
                    if (!isRepeatExcute()) {
                        sendLiveEventData(APP_UNINSTALLED, packageName)
                    }
                }
                // 应用更新或安装
                Intent.ACTION_PACKAGE_ADDED, Intent.ACTION_PACKAGE_REPLACED -> {
                    sendLiveEventData(APP_INSTALLED, packageName)
                }
            }
        }

    }


}
